import pandas as b

# 异常数据的处理
x = b.Series([2,3,4,1,25,3,6,4,9,5,3,4,2,1,2])
print("x:\n",x)
y = x.unique()
print("y:\n",y)

a = b.DataFrame({
    'a':[1,1,3,2],
    'b':[1,1,6,4],
    'c':[1,1,3,9]
})
print("a:\n",a)

a.drop_duplicates()
print("a:\n",a)

a.dropna(how='any')
print("a:\n",a)


# 运用正则处理数据
x = 13579
a = "%o" %x
b = "%x" %x
c = "%e" %x
print("a:\n",a)
print("b:\n",b)
print("c:\n",c)

print("Hello,%s and %s." %("A","B"))

print("%s" %67)
print("%s" %[1,3,5])
print('{:10}'.format("Hello"))
print('{0} to {1:2f}'.format(3.14159,3.14159))
print('{0:0.52} to {0:5.2f}'.format(3.14159))
print("int:{0:d};hex:{0:x};oct:{0:o};bin:{0:b}".format(123))
print("The number {0} in hex is:{0:#x},the number {1} in oct is {1:#o}".format(1111,22))
print('{:<20}'.format("Hello world"))
print('{:>20}'.format("Hello world"))
print('{:^20}'.format("Hello world"))
print('{:*^20}'.format("Hello world"))
print('{:0=20}'.format(1234))
print("{0:%}".format(20))
print("my name is {name},my age is {age},and my QQ is {qq}".format(name="xxx",age=22,qq="88888888"))
name = 'Chen'
age = 25
f'My name is {name},my age is {age} years old'
print(f'My name is {name},my age is {age} years old')
width=10
precision=4
value=11/3
f'{value:{width}.{precision}}'
print(f'{value:{width}.{precision}}')
a=10
b=5
c=3
x=3.1415926
f'{x:{a}.{b}}'
f'{x:{a}.{c}f}'
print(f'{x:{a}.{b}}')
print(f'{x:{a}.{c}f}')


import re
print(re.match('www','www.baidu.com').span())
print(re.match('com','www.baidu.com'))

line="Cats are smarter than dogs"
matchObj=re.match(r'(.*) are (.*?) .*',line,re.M|re.I)
if matchObj:
    print("matchObj.group() : ",matchObj.group())
    print("matchObj.group(1) : ",matchObj.group(1))
    print("matchObj.group(2) : ",matchObj.group(2))
    print("No match!!")


print(re.match('www','www.baidu.com').span())
print(re.match('com','www.baidu.com'))


phone = "2020-221-342"
num = re.sub(r'#.*$',"",phone)
num=re.sub(r'\D',"",phone)
pattern = re.compile(r'\d+')
result1=pattern.findall('runoob 123 google 456')
result2=pattern.findall('run88oob123google456',0,10)
it = re.finditer(r"\d+","12a32bc43jf3")
for match in it:
    print (match.group() )
re.split('\W+','world,world,world.')
re.split('\W+','world,world,world.',1)
re.split('a*','hello world')


#聚合、分组、变化、规约
import numpy as a
import pandas as b

data = b.DataFrame({'A':['a','b','c','a','b'],'B':[1,3,5,7,9]})
print("data:\n", data)
combine = data["B"].groupby(data["A"])
print(combine.mean())
print(combine.size())
combine=data.groupby(data.dtypes)
print(combine.size())
print(data.dtypes)
print(dict(list(combine)))


data = b.DataFrame({'A':['a','b','c','a','b'],'B':[1,3,5,7,9]})
newdata=data['B'].groupby(data['A'])
print(newdata.agg('mean'))
print(newdata.agg(['mean','sum','std']))



data = b.DataFrame({'A':['a','b','c','a','b'],'B1':[3,5,6,8,9],'B2':[2,5,9,6,8]})
newdata=data.groupby('A')
print(newdata.agg({'B1':'mean','B2':'sum'}))


#离散化-等宽法
import pandas as b
ages = [31,27,11,38,15,74,44,32,54,63,41,23]
bins = [15,25,45,65,100]
group_names = ["A","B","C","D"]
personType = b.cut(ages,bins,labels=group_names)
print(personType)